<?php

namespace App\VO\ConvertibleCardUseRecord;

use App\BO\ConvertibleCard\Constant as ConvertibleCardConstant;
use App\BO\ConvertibleCard\Helper as ConvertibleCardHelper;
use App\Entities\ConvertibleCardEntity;
use App\Entities\UserConvertibleCardEntity;
use App\VO\Interfaces\IVODetail;
use Illuminate\Support\Carbon;

class VODetail implements IVODetail
{
    use CommonTrait;

    // public $id = 0;
    // public $title = '';
    // public $typeName = '';
    // public $value = 0;//使用次数/面值
    // public $price = 0;//分
    // public $soldCount = 0;
    // public $statusName = '';
    // public $endSaleDate = '';
    // public $durationDays = 0;
    // public $daysBeforeExpireStr = '';
    // public $createTime = '';
    // public $operator = '';
    // public $detailImage = '';
    public $title = '';
    public $bannerImage = '';
    public $price = 0;
    public $expireDate = '';
    public $remain = 0;
    public $phonenumber = '';
    public $recordList = [];

    public function __construct(array $data)
    {
        $this->phonenumber = ConvertibleCardConstant::CUSTOMERSERVICE_PHONENUMBER;

        if (isset($data['cardentity']) && $data['cardentity'] instanceof ConvertibleCardEntity) {
            $cardEntity = $data['cardentity'];
            $this->bannerImage = $cardEntity->getBanner();
            $this->price = $this->priceConvert($cardEntity);
            $this->title = $cardEntity->getTitle();

            if (isset($data['usercardentity']) && $data['usercardentity'] instanceof UserConvertibleCardEntity) {
                $userCardEntity = $data['usercardentity'];
                $this->expireDate = $userCardEntity->getExpireDate();
                $this->remain = ConvertibleCardHelper::userCardRemain($userCardEntity);
            }

            // $this->detailImage = $cardEntity->getDetailImage();
            // $this->createTime = $cardEntity->getCreatedAt();
            // if ($this->createTime instanceof Carbon) {
            //     $this->createTime = $this->createTime->toDateTimeString();
            // }
            // $this->operator = $cardEntity->getOperator();
            // $endSaleTimestamp = strtotime($cardEntity->getEndSale());
            // $time = time();
            // if ($endSaleTimestamp > $time) {
            //     $days = ceil(($endSaleTimestamp - $time) / 86400);
            //     $this->daysBeforeExpireStr = $days. '天后下架';
            // }
        }
        if (isset($data['recordlist']) && $data['recordlist'] && is_array($data['recordlist'])) {
            $listData = Builder::buildList($data['recordlist']);
            $this->recordList = $listData->list;
        }
    }
}
